package com.lagou.edu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

@Configuration
public class MyLocalResovel implements LocaleResolver {

    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResovel();
    }


    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        String header = httpServletRequest.getHeader("Accept-Language");
        Locale locale=null;
        if(!StringUtils.isEmpty(l)){

            String[] split = l.split("_");
            locale=new Locale(split[0],split[1]);
        }else{
            //Accept-Language: zh-CN,zh;q=0.9
            String[] split = header.split(",");
            String[] localSplit = split[0].split("-");
            locale=new Locale(localSplit[0],localSplit[1]);
        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
